package pkgBFPaint;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class DrawRectangle extends Action{
	private int width, high, fromx,fromy,tox,toy;
	private Point from,to;

	DrawRectangle(Color _color) {
		color = _color;
		actionID = ActionID.DRAWRECTANGLE;
	}

	DrawRectangle() {
		actionID = ActionID.DRAWRECTANGLE;
	}
	
	public void manageNegativeDimension(){
				
		if( width < 0 ){
			fromx=tox;
			width = Math.abs(width);
		}
		
		if( high < 0 ){
			fromy=toy;
			high = Math.abs(high);
		}
	}

	public void draw(Graphics g) {

		int i;
		
		g.setColor(color);

		if (points == null) {
			return;
		}

		for(i=0;(i+1) < points.size(); i++ ){
			from= points.get(i);
			fromx = from.x;
			fromy= from.y;
			i++;
			to = points.get(i);
			tox=to.x;
			toy=to.y;
			width = tox-fromx;
			high = toy-fromy;
			manageNegativeDimension();
			g.drawRect(fromx,fromy,width,high);
		}
	}

}
